//
//  BQListViewController.m
//  BeeQuick
//
//  Created by AaronWei on 2017/7/9.
//  Copyright © 2017年 BecomeRich. All rights reserved.
//

#import "BQListViewController.h"
#import "UILabel+Addition.h"
#import "UIColor+MLAddition.h"
#import "BQAccountManager.h"
#import "BQListViewModel.h"
#import "BQCategoryModel.h"
#import "BQCategoryTableViewCell.h"
#import "BQProductTableViewCell.h"

@interface BQListViewController () <UITableViewDelegate, UITableViewDataSource>

@property (copy, nonatomic) NSArray<BQCategoryModel *> *categories;

@property (weak, nonatomic) UIView *backgroundView;

@property (weak, nonatomic) UITableView *categoryTableView;

@property (weak, nonatomic) UITableView *productTableView;

@end

static NSString *category_id = @"category";
static NSString *product_id = @"product";

@implementation BQListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:self.view.bounds];
    
    _backgroundView = backgroundView;
    
    backgroundView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:backgroundView];
    
    [self loadData];
    
}

- (void)loadData {
    
    [SVProgressHUD showWithStatus:@"正在玩命加载中..."];
    
    [[BQListViewModel sharedViewModel] setSuccessBlock:^(NSArray *categories){
        
        self.categories = categories;
        
        [SVProgressHUD dismiss];
        
        [self.backgroundView removeFromSuperview];
        
        [self.categoryTableView reloadData];
        
        [self.productTableView reloadData];
        
        [self.categoryTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
        
    }];
    
}

- (void)setupTableView {
    
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    _categoryTableView = [self creatTableView];
    
    _productTableView = [self creatTableView];
    
    _categoryTableView.backgroundColor = [UIColor ml_colorWithHex:0xEFEFEF];
    
    _categoryTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 8);
    
    _productTableView.separatorInset = UIEdgeInsetsZero;
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.extendedLayoutIncludesOpaqueBars = YES;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _productTableView.rowHeight = 100;
    
    _productTableView.sectionHeaderHeight = 20;
    
    [_categoryTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.offset(0);
        make.width.mas_equalTo(100);
    }];
    
    [_productTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.offset(0);
        make.leading.equalTo(_categoryTableView.mas_trailing);
    }];
    
    [_categoryTableView registerClass:[BQCategoryTableViewCell class] forCellReuseIdentifier:category_id];
    
    [_productTableView registerNib:[UINib nibWithNibName:@"BQProductTableViewCell" bundle:nil] forCellReuseIdentifier:product_id];
    
}

- (UITableView *)creatTableView {
    
    UITableView *tableView = [UITableView new];
    
    tableView.delegate = self;
    
    tableView.dataSource = self;
    
    [self.view addSubview:tableView];
    
    return tableView;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UILabel *textLabel = [UILabel labelWithTextColor:[UIColor darkGrayColor] andTextSize:18 andTextContent:[NSString stringWithFormat:@"  %@", self.categories[section].name]];
    
    textLabel.backgroundColor = [UIColor ml_colorWithHex:0xFAFAFA];
    
    textLabel.alpha = 0.5;
    
    return textLabel;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == _categoryTableView) {
        
        NSInteger index = indexPath.row;
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:index];
        
        [_productTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
        
    } else {
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
    }
    
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.isDragging || tableView.isDecelerating || tableView.isTracking) {
        
        if (tableView == _productTableView) {
            
            NSArray *indexPathArray = [tableView indexPathsForVisibleRows];
            
            NSIndexPath *indexPath = indexPathArray.firstObject;
            
            NSIndexPath *leftIndexPath = [NSIndexPath indexPathForRow:indexPath.section inSection:0];
            
            [_categoryTableView selectRowAtIndexPath:leftIndexPath animated:NO scrollPosition:UITableViewScrollPositionTop];
            
        }
        
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return tableView == _categoryTableView ? 1 : self.categories.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return tableView == _categoryTableView ? self.categories.count : self.categories[section].products.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == _categoryTableView) {
        BQCategoryTableViewCell *categoryCell = [tableView dequeueReusableCellWithIdentifier:category_id forIndexPath:indexPath];
        
        categoryCell.textLabel.text = self.categories[indexPath.row].name;
        
        return categoryCell;
        
    } else {
        
        BQProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:product_id forIndexPath:indexPath];
        
        cell.model = self.categories[indexPath.section].products[indexPath.row];
        
        return cell;
        
    }
    
}

@end
